package com.eplayer.mqdx.activity;

import com.eplayer.mqdx.R;
import com.eplayer.mqdx.fragment.CollegeFragment;
import com.eplayer.mqdx.fragment.FindFragment;
import com.eplayer.mqdx.fragment.HomeFragment;
import com.eplayer.mqdx.fragment.MeFragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class MainActivity extends FragmentActivity {
	private FrameLayout fl_content;
	private RadioGroup rg_main_line;
	private RadioButton rb_main_home, rb_main_college, rb_main_find,
			rb_main_me;
	private HomeFragment homeFragment;
	private CollegeFragment collegeFragment;
	private FindFragment findFragment;
	private MeFragment meFragment;
    private boolean isPressedBackOnce = false;
    private long first = 0;
    private long second = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		initView();
		initDate();
	}

	private void initView() {
		fl_content = (FrameLayout) findViewById(R.id.fl_content);
		rg_main_line = (RadioGroup) findViewById(R.id.rg_mian_line);
		rb_main_home = (RadioButton) findViewById(R.id.rb_main_home);
		rb_main_college = (RadioButton) findViewById(R.id.rb_main_college);
		rb_main_find = (RadioButton) findViewById(R.id.rb_main_find);
		rb_main_me = (RadioButton) findViewById(R.id.rb_main_me);

		homeFragment = new HomeFragment();
		collegeFragment = new CollegeFragment();
		findFragment = new FindFragment();
		meFragment = new MeFragment();

		// 设置默认页面
		getSupportFragmentManager().beginTransaction()
				.add(R.id.fl_content, homeFragment).commit();
		rb_main_home.setChecked(true);

	}

	private void initDate() {
		rg_main_line.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.rb_main_home:
					getSupportFragmentManager().beginTransaction()
							.replace(R.id.fl_content, homeFragment).commit();
					break;
				case R.id.rb_main_college:
					getSupportFragmentManager().beginTransaction()
					.replace(R.id.fl_content, collegeFragment).commit();
					break;
				case R.id.rb_main_find:
					getSupportFragmentManager().beginTransaction()
					.replace(R.id.fl_content, findFragment).commit();
					break;
				case R.id.rb_main_me:
					getSupportFragmentManager().beginTransaction()
					.replace(R.id.fl_content, meFragment).commit();
					break;

				default:
					break;
				}

			}
		});
	}
	@Override
	public void onBackPressed() {
		if (isPressedBackOnce) {
			//已经点击了一次
			second = System.currentTimeMillis();
			if (second-first>2000) {
				//重新进行第一次点击
				Toast.makeText(this, "再点一次退出", 0).show();
				isPressedBackOnce = true;
				first = System.currentTimeMillis();
			} else {
                //直接退出
				finish();
				isPressedBackOnce = false;
				first =0;
				second=0;
			}
		}else {
			//没有点击
			Toast.makeText(this, "再点一次退出", 0).show();
			isPressedBackOnce = true;
			first = System.currentTimeMillis();
					
		}
	}

}
